package java2020spring;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
public class Window extends JFrame{
	int windowsNum=0;
	JMenuBar bar;
	JMenu file,edit,geshi,look,help;
	JMenuItem create,open,save,lingcun,exit;//文件菜单栏的功能
	JMenuItem chexiao,cut,copy,zhantie,delete;//编辑菜单栏的功能
	JCheckBoxMenuItem autoNextLine;//格式中的自动换行
	JCheckBoxMenuItem condition;//查看中的状态栏
	JMenuItem lookForHelp,about;//帮助中的功能
		//状态栏
	JLabel conditionTxt,labelTips;
	JFileChooser fileDialog;
	BufferedReader in;
	FileReader fileReader;
	BufferedWriter out;
	FileWriter fileWriter;
	JTextArea scanArea;
	UndoManager manager;

	EditListener editListener;
	JPopupMenu popup;
	Window(){
		setLayout(new BorderLayout());
		setBounds(30,30,450,500);
		setVisible(true);
		setTitle("最大化窗口以开始");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		init();
	}
	void init() {
		manager=new UndoManager();
		scanArea=new JTextArea();
		editListener=new EditListener();

		fileDialog=new JFileChooser();
		scanArea.setFont(new Font(null,0,20));
		scanArea.setLineWrap(true);
		this.add(new JScrollPane(scanArea),BorderLayout.CENTER);
		scanArea.setPreferredSize(new Dimension(1500,760));
		scanArea.getDocument().addUndoableEditListener(manager);
		//以上部分为文本区域
		//以下部分是菜单
		bar=new JMenuBar();
		file=new JMenu("文件");
		edit=new JMenu("编辑");
		geshi=new JMenu("格式");
		look=new JMenu("查看");
		help=new JMenu("帮助");
		create=new JMenuItem("新建");
		open=new JMenuItem("打开");
		save=new JMenuItem("保存");
		lingcun=new JMenuItem("另存为");
		exit=new JMenuItem("退出");
		chexiao=new JMenuItem("撤销");
		cut=new JMenuItem("剪切");
		copy=new JMenuItem("复制");
		zhantie=new JMenuItem("粘贴");
		delete=new JMenuItem("删除");
		autoNextLine=new JCheckBoxMenuItem("自动换行",true);
		condition=new JCheckBoxMenuItem("状态栏",true);
		lookForHelp=new JMenuItem("查看帮助");
		about=new JMenuItem("关于记事本");
		bar.add(file);
		bar.add(edit);
		bar.add(geshi);
		bar.add(look);
		bar.add(help);
		file.add(create);//文件
		file.add(open);
		file.add(save);
		file.add(lingcun);
		file.addSeparator();
		file.add(exit);
		create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK));
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_DOWN_MASK));
		edit.add(chexiao);//编辑
		edit.add(cut);
		edit.add(copy);
		edit.add(zhantie);
		chexiao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_DOWN_MASK));
		cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_DOWN_MASK));
		copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
		zhantie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
		edit.add(delete);
		geshi.add(autoNextLine);//格式
		look.add(condition);//查看
		help.add(lookForHelp);//帮助
		help.addSeparator();
		help.add(about);
		setJMenuBar(bar);
		//这里开始添加功能
			//编辑(Edit)的功能
		copy.setActionCommand("copy");
		zhantie.setActionCommand("zhantie");
		cut.setActionCommand("cut");
		chexiao.setActionCommand("chexiao");
		copy.addActionListener(editListener);
		zhantie.addActionListener(editListener);
		cut.addActionListener(editListener);
		chexiao.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
				if(manager.canUndo())
					manager.undo();
			}
		});
		copy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==copy)
					scanArea.copy();
				}
		});
		zhantie.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==zhantie)
					scanArea.paste();
			}
		});
		cut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==cut)
					scanArea.cut();
			}
		});
		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(about, "              这是一个比较初级的记事本","关于记事本",JOptionPane.PLAIN_MESSAGE);
			}
		});
		lookForHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(lookForHelp, "它跟你经常用的记事本差不多啦\n不过功能更少","查看帮助",JOptionPane.PLAIN_MESSAGE);
			}
		});
		//我们从这里开始编辑"文件"的功能
		FileNameExtensionFilter filter=new FileNameExtensionFilter("文本文件", "txt");
		fileDialog.setFileFilter(filter);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==save) {
					int state=fileDialog.showSaveDialog(null);
					if(state==JFileChooser.APPROVE_OPTION) {
						try { 
							File dir=fileDialog.getCurrentDirectory();
							String name=fileDialog.getSelectedFile().getName();
							File file=new File(dir,name+".txt");
							fileWriter=new FileWriter(file);
							out=new BufferedWriter(fileWriter);
							out.write(scanArea.getText());
							out.close();
							fileWriter.close();
						}
						catch(IOException exp) {}
					}
				}
			}
		});
		open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==open) {
					int state=fileDialog.showOpenDialog(null);
					if(state==JFileChooser.APPROVE_OPTION) {
						scanArea.setText(null);
						try { 
							File dir=fileDialog.getCurrentDirectory();
							String name=fileDialog.getSelectedFile().getName();
							File file=new File(dir,name);
							fileReader=new FileReader(file);
							in=new BufferedReader(fileReader);
							String s=null;
							while((s=in.readLine())!=null) {
								scanArea.append(s+"\n");
							}
							in.close();
							fileReader.close();
						}
						catch(IOException exp) {}
					}
				}
			}
		});
		lingcun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==save) {
					int state=fileDialog.showSaveDialog(null);
					if(state==JFileChooser.APPROVE_OPTION) {
						try { 
							File dir=fileDialog.getCurrentDirectory();
							String name=fileDialog.getSelectedFile().getName();
							File file=new File(dir,name+".txt");
							fileWriter=new FileWriter(file);
							out=new BufferedWriter(fileWriter);
							out.write(scanArea.getText());
							out.close();
							fileWriter.close();
						}
						catch(IOException exp) {}
					}
				}
			}
		});
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==exit){
					System.exit(0);
				}
			}
		});
		create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setTitle("无标题");
				scanArea.setText(null);
			}
		});
		// 状态栏
		conditionTxt=new JLabel("第1行,第1列");
		add(conditionTxt,BorderLayout.SOUTH);
		scanArea.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				try {
					int pos=scanArea.getCaretPosition();
					int line=scanArea.getLineOfOffset(pos)+1;
					int hang=pos-scanArea.getLineStartOffset(line-1)+1;
					conditionTxt.setText("第"+line+"行"+"第"+hang+"列");
				}
				catch(Exception ex) {
					labelTips.setText("无法获取当前光标位置");
				} 
			}
		});
		condition.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(condition.getState()==true)
					conditionTxt.setVisible(true);
				else
					conditionTxt.setVisible(false);
		
			}
		});
		//右键弹出菜单
		JMenuItem che=new JMenuItem("撤销");
		che.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(manager.canUndo())
					manager.undo();
			}
		});
		JMenuItem cut=new JMenuItem("剪切");
		cut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				scanArea.cut();
			}
		});
		JMenuItem fuzhi=new JMenuItem("复制");
		fuzhi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				scanArea.copy();
			}
		});
		JMenuItem paste=new JMenuItem("粘贴");
		paste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				scanArea.paste();
			}
		});
		popup=new JPopupMenu("弹出式菜单demo");
		popup.setVisible(true);
		popup.add(che);
		che.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_DOWN_MASK));
		popup.addSeparator();
		popup.add(cut);
		cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
		popup.add(fuzhi);
		fuzhi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
		popup.add(paste);
		paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
		addMouseListener(new MouseAdapter()  {
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if(e.getButton()==MouseEvent.BUTTON3) {
					popup.show(e.getComponent(),e.getX(),e.getY());
				}
			}
		}); 
		scanArea.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if(e.getButton()==MouseEvent.BUTTON3) {
					popup.show(e.getComponent(),e.getX(),e.getY());
				}
			}
		}); 
		autoNextLine.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(autoNextLine.getState()==true)
					scanArea.setLineWrap(true);
				else
					scanArea.setLineWrap(false);
			}
		});
		
	}
}